package main;

import java.util.ArrayList;
import java.util.Map.Entry;

import org.jdom.Element;

import script.ScriptJava;
import ui.MainWindow;
import config.EConfig;
import convert.BaseContent;
import convert.EWorkBook;

public class ExcelConvert 
{	
	private static ArrayList<String> m_excels = new ArrayList<>();
	
	public static void main(String[] args)
	{
		try 
		{
			if(args.length < 1)
			{
				Common.m_hasUI = true;
				MainWindow.instance();
			}
			else
			{
				boolean isConfig = false;
				String configSheet = Def.DEFAULT_CONFIG;
				for(String str: args)
				{
					if(str.compareTo("-c") == 0)
					{
						isConfig = true;
						continue;
					}
					else if(isConfig)
					{
						configSheet = str;
						isConfig = false;
					}
					else
					{
						readExcelFile(str, configSheet);
					}
				}
				run();
			}
//			Workbook wb = new Workbook("test.xls");
//			wb.test();
		} 
		catch(Exception e) 
		{
			//e.printStackTrace();
			Common.print(e.toString());
		}
	}
	
	public static void clear()
	{
		m_excels.clear();
		EWorkBook.m_root = new BaseContent("", null);
		ScriptJava.m_scripts.clear();
		EConfig.m_groups.clear();
		EConfig.m_preScript.clear();
//		ScriptTable.m_currentRow = 0;
//		ScriptRow.m_currentCell = 0;
//		Flow.m_currentFlow = null;
//		Flow.m_stack.clear();
	}
	
	public static void readExcelFile(String path, String configSheet) throws ErrorException
	{
		if(!m_excels.contains(path))
		{
			EWorkBook file = new EWorkBook(path, configSheet);	
			file.init();
			m_excels.add(path);
		}
	}
	
	public static void addConfig(String[] paths)
	{
		if(paths.length <= 0)
		{
			return;
		}
		for(String path: paths)
		{
			try 
			{
				EWorkBook file = new EWorkBook(path, Def.DEFAULT_CONFIG);
				file.addConfigSheet();
				file.saveToExcel();
			} 
			catch (Exception e) 
			{
				Common.print("add config sheet error " + e.toString());
			}
		}
		EWorkBook.m_root = new BaseContent("", null);
		Common.print("finish adding");
	}

	public static void run() throws ErrorException
	{
		int len = EWorkBook.m_root.getChildNumber();
		for(int i = 0; i < len; ++i)
		{
			EWorkBook exc = (EWorkBook)EWorkBook.m_root.getChild(i);
			EConfig.clearConfig();
			exc.applyConfig();
			for(Entry<String, Integer> entry: EConfig.m_preScript.entrySet())
			{
				ScriptJava script = ScriptJava.m_scripts.get(entry.getKey());
				if(script == null)
				{
					throw new ErrorException(ErrorDefine.E_NO_PRE_SCRIPT_ERR, entry.getKey());
				}
				script.run();
			}
			exc.toXml();
			exc.saveToXml();
		}
		for(Entry<String, ArrayList<String>> entry: EConfig.m_groups.entrySet())
		{
			Element root = new Element(Def.CONFIG_GROUP);
			root.setAttribute(Def.NAME, entry.getKey());
			String path = "";
			for(String str: entry.getValue())
			{
				ArrayList<BaseContent> list = EWorkBook.m_root.getChildrenByName(str);
				if(list == null)
				{
					Common.print("group member can't find:" + str);
					continue;
				}
				for(BaseContent bc: list)
				{
					EWorkBook ew = (EWorkBook)bc;
					if(path.length() == 0)
					{
						path = ew.getDirPath();
					}
					EConfig.clearConfig();
					ew.applyConfig();
					root.addContent(ew.toXml());
				}
			}
			EWorkBook.saveToXml(root, path + entry.getKey() + ".xml");
		}
		for(Entry<String, ScriptJava> entry: ScriptJava.m_scripts.entrySet())
		{
			if(!EConfig.m_preScript.containsKey(entry.getKey()))
			{
				entry.getValue().run();
			}
		}
		Common.print("finish");
	}
}
